package com.waterworld.watch.mine.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import com.alibaba.fastjson.JSONObject;
import com.waterworld.watch.R;
import com.waterworld.watch.common.application.MyApplication;
import com.waterworld.watch.common.db.DbController;
import com.waterworld.watch.common.db.Notification;
import com.waterworld.watch.common.net.BaseObserverListener;
import com.waterworld.watch.common.util.TimeUtils;
import com.waterworld.watch.mine.manager.MineManager;
import com.waterworld.watch.push.contant.MessageContants;

import java.util.List;

public class NotificationAdapter extends RecyclerView.Adapter<NotificationAdapter.MyHolder>{

    public static final int NOTIFICATION_UNREAD = 0;
    public static final int NOTIFICATION_READ = 1;
    public static final int NOTIFICATION_DELETE = 2;

    private Context mContext;
    private List<Notification> mList;
    DbController dbController = DbController.getInstance(MyApplication.getContext());
    private JSONObject json;

    public NotificationAdapter(Context mContext, List<Notification> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new MyHolder(LayoutInflater.from(mContext).inflate(R.layout.item_mine_message_notification, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
        json = (JSONObject) JSONObject.parse(mList.get(i).getContent());
        switch (mList.get(i).getState()) {
            case NOTIFICATION_UNREAD:
                myHolder.isRead.setVisibility(View.VISIBLE);
                myHolder.time.setText(TimeUtils.getMonthAndDay(mList.get(i).getTime()));
                setContent(i,myHolder);
                break;
            case NOTIFICATION_READ:
                myHolder.isRead.setVisibility(View.GONE);
                myHolder.time.setText(TimeUtils.getMonthAndDay(mList.get(i).getTime()));
                setContent(i,myHolder);
                break;
            case NOTIFICATION_DELETE:
                break;
        }
    }

    private void setContent(int i,MyHolder myHolder) {
        switch (mList.get(i).getMsgType()) {
            case MessageContants.WATCH_SHUTDOWN:
                myHolder.content.setText(String.format(mContext.getString(R.string.notification_shut_down), mList.get(i).getWatchUserName(), mList.get(i).getWatchUserPhone().substring(7)));
                break;
            case MessageContants.WATCH_BIND:
                myHolder.content.setText(String.format(mContext.getString(R.string.notification_watch_bind), json.getString("phone"), mList.get(i).getWatchUserName(), json.getString("relationWithBaby")));
                break;
            case MessageContants.SECURITY_FENCE:
                if (json.getString("remindWay") != null) {
                    if (json.getString("remindWay").equals("1")) {
                        myHolder.content.setText(String.format(mContext.getString(R.string.notification_secret_fence), mList.get(i).getWatchUserName(), mList.get(i)
                                .getWatchUserPhone().substring(7), TimeUtils.getHourAndMin(json.getString("uploadTime")) + "进入", json.getString("fenceName")));
                    } else if (json.getString("remindWay").equals("2")) {
                        myHolder.content.setText(String.format(mContext.getString(R.string.notification_secret_fence), mList.get(i).getWatchUserName(), mList.get(i)
                                .getWatchUserPhone().substring(7), TimeUtils.getHourAndMin(json.getString("uploadTime")) + "离开", json.getString("fenceName")));
                    } else if (json.getString("remindWay").equals("3")) {
                        myHolder.content.setText(String.format(mContext.getString(R.string.notification_secret_fence), mList.get(i).getWatchUserName(), mList.get(i)
                                .getWatchUserPhone().substring(7), TimeUtils.getHourAndMin(json.getString("uploadTime")) + "未进入", json.getString("fenceName")));
                    }
                }
                break;
            case MessageContants.LOW_POWER:
                myHolder.content.setText(String.format(mContext.getString(R.string.notification_low_power), mList.get(i).getWatchUserName(), mList.get(i).getWatchUserPhone().substring(7)));
                break;
            case MessageContants.SIM_PULL_OUT:
                myHolder.content.setText(String.format(mContext.getString(R.string.notification_sim_pullout), mList.get(i).getWatchUserName(), mList.get(i).getWatchUserPhone().substring(7)));
                break;
            case MessageContants.URGENT_CALL:
                myHolder.content.setText(String.format(mContext.getString(R.string.notification_urgent_call), mList.get(i).getWatchUserName(), mList.get(i).getWatchUserPhone().substring(7), json.getString("urgentCall")));
                break;
            case MessageContants.WATCH_OFFLINE:
                myHolder.content.setText(String.format(mContext.getString(R.string.notification_watch_offline), mList.get(i).getWatchUserName(), mList.get(i).getWatchUserPhone().substring(7)));
                break;
        }
        myHolder.parent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mList.get(i).setState(1);
                dbController.updateNotification(mList.get(i));
                notifyItemChanged(i);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (mList.get(position).getState() == NOTIFICATION_UNREAD) {
            return mList.get(position).getMsgType();
        } else {
            return 0;
        }
    }

    class MyHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.rl_parent)
        RelativeLayout parent;

        @BindView(R.id.tv_content)
        TextView content;

        @BindView(R.id.tv_detail)
        TextView detail;

        @BindView(R.id.tv_time)
        TextView time;

        @BindView(R.id.isRead)
        ImageView isRead;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
